package the.workshop.bu.controller;

import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.workshop.bu.dto.CreditCondition;
import the.workshop.bu.dto.CreditVO;
import the.workshop.bu.dto.req.CreditAddReq;
import the.workshop.bu.dto.req.CreditModifyReq;
import the.workshop.bu.service.ICreditService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 申贷控制器
 *
 * @author lei.liu
 * @since 2020-06-15 15:20:23
 */
@RestController
@RequestMapping("/bu/credit")
public class CreditController {

    @Resource
    private ICreditService creditService;

    @PostMapping("/add")
    public APIResult add(@ModelAttribute CreditAddReq req) {
        creditService.save(req.toCreditDTO());
        return APIResult.success();
    }

    @PostMapping("/modify")
    public APIResult modify(@ModelAttribute CreditModifyReq req) {
        creditService.updateById(req.toCreditDTO());
        return APIResult.success();
    }

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids", required = true) List<String> ids) {
        creditService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @GetMapping("/{creditId}")
    public APIResult get(@PathVariable(value = "creditId") Integer creditId) {
        return APIResult.success(creditService.detailById(creditId));
    }

    @GetMapping("/page")
    public APIResult<QueryResult<CreditVO>> page(
            @ApiParam(name = "keyword", value = "关键字", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(name = "orderBy", value = "排序字段", required = false)
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @ApiParam(name = "pageNum", value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页大小", required = false, defaultValue = "20")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        CreditCondition condition = new CreditCondition();
        condition.setKeyword(keyword);
        condition.setOrderBy(orderBy);
        return APIResult.success(creditService.pageList(condition, pageNum, pageSize));
    }
}